package giit.computerbase.util;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: tum0r
 * Package Name: com.util
 * Time: 2018/10/30 Tuesday 09:39:35
 */
public class DealWithAddress {
    public static String dealWith(String address) throws SocketException {
        if (address != null && !address.equals("")) {
            Pattern pattern = Pattern.compile(address + "[\\d]{0,}");
            Matcher matcher;
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                String text = enumeration.nextElement().getInterfaceAddresses().toString();
                matcher = pattern.matcher(text);
                while (matcher.find()) {
                    if (matcher.group().equals(address)) {
                        return "127.0.0.1";
                    }
                }
            }
        }
        return address;
    }
}
